package com.heima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.PageBean;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.PartnerQueryParamDto;
import com.heima.service.VmTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class VmTypeServiceImpl implements VmTypeService {

    @Autowired
    private VmTypeMapper vmMapper;

    @Override
    public PageBean selectVmList(PartnerQueryParamDto vmDto) {
        PageHelper.startPage(vmDto.getPageIndex(), vmDto.getPageSize());
        List<VmType> list = vmMapper.selectVmList(vmDto);
        PageInfo<VmType> info = new PageInfo<>(list);

        return new PageBean(vmDto.getPageIndex(),vmDto.getPageSize(),
                (long) info.getPages(),info.getTotal(),info.getList());
    }

    @Override
    public Boolean addVmType(VmType vmType) {
        if (vmMapper.selectVmListByModelAndName(vmType) != null){
            return  false;
        }
        vmMapper.addVmType(vmType);
        return true ;
    }

    @Override
    public Boolean updateVmType(VmType vmType) {
        //判断 是不是名字没变 (偷个懒，这里就不判断了) 正常需要先判断，是不是名字和model没变，如果没变，自己改自己可以改，如果变了，该别人不能改
        vmMapper.updateVmType(vmType);
        return true;
    }

    @Override
    public Boolean deleteVmType(int typeId) {
         if (vmMapper.selectVendingMachineBytypeId(typeId)>0){
             return false;
         }
        vmMapper.deleteVmType(typeId);
        return true ;
    }
}
